//
//  ZYFangkeView.swift
//  ZYweibo
//
//  Created by 张耀 on 16/6/26.
//  Copyright © 2016年 zhangyao. All rights reserved.
//

import UIKit
import SnapKit

class ZYFangkeView: UIView {
    
    //button按钮点击闭包
    var cloure:(() ->())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -视图设置
    
    private func setupUI(){
    
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        //添加控件
        addSubview(yuanImageView)
        addSubview(zhezhaoImageView)
        addSubview(fangziImageView)
        addSubview(wenziLabel)
        addSubview(dengluButton)
        addSubview(zhuceButton)
        
        //添加约束
        
        //圆圈
        yuanImageView.snp_makeConstraints { (make)-> Void in
            make.center.equalTo(self)
            
        }
        
        //遮罩
        zhezhaoImageView.snp_makeConstraints { (make)-> Void in make.center.equalTo(self)
        }

        //房子
        fangziImageView.snp_makeConstraints { (make)-> Void in
            make.center.equalTo(self)
        }

        //文字
        wenziLabel.snp_makeConstraints { (make)-> Void in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(zhezhaoImageView.snp_bottom).offset(16)
            make.centerX.equalTo(zhezhaoImageView)
        }

        //登录
        dengluButton.snp_makeConstraints { (make)-> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(wenziLabel)
            make.top.equalTo(wenziLabel.snp_bottom).offset(16)
        }

        //注册
        zhuceButton.snp_makeConstraints { (make)-> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(wenziLabel)
            make.top.equalTo(dengluButton)
        }

    }
    
    // MARK: -设置动画
    func donghua(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画时间
        anim.duration = 10
        // 设置tovalue
        anim.toValue = M_PI * 2
        // 动画次数
        anim.repeatCount = MAXFLOAT
        // 当我们app推到后台或者切换控制器的时候 动画默认完成
        anim.removedOnCompletion = false
        // 添加动画
        yuanImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // MARK: - 懒加载控件
    
    //圆圈
    private lazy var yuanImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var zhezhaoImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子
    private lazy var fangziImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    //文字
    private lazy var wenziLabel: UILabel = {
    
        let lab = UILabel()
        
        lab.textColor = ZhuColor
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(14)
        lab.textAlignment = .Center
    
        return lab
    }()
    
    // MARK: -登录
    lazy var dengluButton: UIButton = {
    
        let button = UIButton()
        
        //添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(ZhuColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
       
        return button
    
    }()
    
    // MARK: -注册
    lazy var zhuceButton:UIButton = {
        
        let button = UIButton()
    
        //添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ZhuColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)

        return button
    }()

    // MARK: - 供外界调用的方法
    func setupVisitorView(imgName: String?, title: String?){
        // 如果有值就代表不是主页
        if let img = imgName, let tit = title {
            fangziImageView.image = UIImage(named: img)
            wenziLabel.text = tit
            // 隐藏圆圈
            yuanImageView.hidden = true
        }else {
            // 代表是主页
            donghua()
        }
    }

    @objc private func buttonClick(){
        // 执行闭包
        cloure?()
    }

}
extension ZYFangkeView{

    func demo(){
    
        //取消Autoresizing
        yuanImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: yuanImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: yuanImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

    
    }

}











